package com.minhua.jijiehao2.mainframe.circle.productServiceActivity.impl

import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.mainframe.circle.productServiceActivity.imp.IProductServiceView
import com.minhua.jijiehao2.utils.AfterTextWatcher
import com.minhua.jijiehao2.utils.T
import kotlinx.android.synthetic.main.activity_product_service.*

class ProductServiceActivity : BaseActivity<IProductServiceView, ProductServicePresenter>(), IProductServiceView {

    override fun getLayoutId(): Int = R.layout.activity_product_service

    override fun getPresenter(): ProductServicePresenter = ProductServicePresenter()

    companion object {
        fun intentStart(activity: Activity, title: String, content: String, requestCode: Int) {
            val intent = Intent(activity, ProductServiceActivity::class.java)
            intent.putExtra("title", title)
            intent.putExtra("content", content)
            activity.startActivityForResult(intent, requestCode)
        }
    }

    override fun initView() {
        ctTitle.setTitle(intent.getStringExtra("title"))
        val text = intent.getStringExtra("content")
        tvLength.text = "0/200"
        if (!TextUtils.isEmpty(text)) {
            etIntroduce.setText(text)
            tvLength.text = "${text.length}/200"
        }
        ctTitle.viewHolder.setOnClickListener(R.id.tvRight, {
            if (TextUtils.isEmpty(etIntroduce.text)) {
                T.showShort("介绍不能为空")
            } else {
                val intent = Intent()
                intent.putExtra("text", etIntroduce.text.toString().trim())
                setResult(Activity.RESULT_OK, intent)
                finish()
            }
        })
        etIntroduce.addTextChangedListener(object : AfterTextWatcher() {
            override fun afterTextChanged(s: Editable?) {
                tvLength.text = "${etIntroduce.text.toString().trim().length}/200"
            }
        })
    }

    override fun onBackPressed() {
        if (intent.getStringExtra("text") != etIntroduce.text.toString().trim()) {
            AlertDialog.Builder(this)
                    .setMessage("确定不保存介绍并退出？")
                    .setPositiveButton("确定", { dialog, _ ->
                        dialog.dismiss()
                        finish()
                    })
                    .setNegativeButton("取消", { dialog, _ ->
                        dialog.dismiss()
                    })
                    .create()
                    .show()
        } else {
            finish()
        }
    }
}
